package com.cqeec.gams.controller;

import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.cqeec.gams.common.Function;
import com.cqeec.gams.entity.Role;
import com.cqeec.gams.entity.RolePermissions;
import com.cqeec.gams.service.FunctionalModuleService;
import com.cqeec.gams.service.RoleService;

@Controller
@RequestMapping("/app/role/")
public class RoleController {
	@Autowired
	private RoleService roleService;
	@Autowired
	private FunctionalModuleService fmService;

	@RequestMapping("/delete.html")
	public ModelAndView delete(String id, int page, int size) {
		ModelAndView mav = new ModelAndView("redirect:/app/role/list.html");
		roleService.delete(id);
		mav.addObject("page", page);
		mav.addObject("size", size);
		return mav;
	}

	@RequestMapping("/update.html")
	public ModelAndView update(Role role, String[] functions, int page, int size) {
		ModelAndView mav = new ModelAndView("role/edit");
		mav.addObject("page", page);
		mav.addObject("size", size);
		try {
			roleService.update(role);
			// 更新角色权限
			// 先删除角色原有的权限
			roleService.deleteRolePermissions(role.getId());
			if (functions != null) {
				// 再将角色的权限插入
				RolePermissions rp;
				for (String s : functions) {
					rp = new RolePermissions();
					rp.setId(UUID.randomUUID().toString());
					rp.setRole(role.getId());
					rp.setFunctionalModule(s);
					roleService.inserRolePermission(rp);
				}
			}
			mav.addObject("functions", fmService.getFunction(role.getId()));
			mav.addObject("message", "保存角色成功！");
		} catch (Exception e) {
			e.printStackTrace();
			mav.addObject("functions", fmService.getFunction(role.getId()));
			mav.addObject("message", "保存角色失败！");
		}
		return mav;
	}

	@RequestMapping("/save.html")
	public ModelAndView save(Role role, String[] functions, RedirectAttributes ra) {
		ModelAndView mav = new ModelAndView("role/add");
		String id = role.getId();
		if (id == null || "".equals(id)) {
			role.setId(UUID.randomUUID().toString().toUpperCase());
		}
		try {
			roleService.insert(role);
			if (functions != null) {
				RolePermissions rp;
				for (String s : functions) {
					rp = new RolePermissions();
					rp.setId(UUID.randomUUID().toString());
					rp.setRole(role.getId());
					rp.setFunctionalModule(s);
					roleService.inserRolePermission(rp);
				}
			}
			mav.setViewName("redirect:/app/role/add.html");
			ra.addFlashAttribute("message", "添加角色成功！");
		} catch (Exception e) {
			e.printStackTrace();
			// 把所有功能带添加页面进行授权
			mav.addObject("functions", fmService.getFunction());
			mav.addObject("message", "添加角色失败！");
		}
		return mav;
	}

	@RequestMapping("/edit.html")
	public ModelAndView gotoEditPage(String id, int page, int size) {
		ModelAndView mav = new ModelAndView("role/edit");
		Role role = roleService.findById(id);
		mav.addObject("role", role);
		mav.addObject("page", page);
		mav.addObject("size", size);
		// 把所有功能带添加页面进行授权
		mav.addObject("functions", fmService.getFunction(id));
		return mav;
	}

	@RequestMapping("/add.html")
	public ModelAndView gotoAddPage() {
		ModelAndView mav = new ModelAndView("role/add");
		// 把所有功能带添加页面进行授权
		List<Function> functions = fmService.getFunction();
		mav.addObject("functions", functions);
		return mav;
	}

	@RequestMapping("/list.html")
	public ModelAndView gotoMainPage(String page, String size) {
		ModelAndView mav = new ModelAndView("role/list");
		int iPage, iSize;
		if (page == null || "".equals(page)) {
			iPage = 1;
		} else {
			iPage = Integer.parseInt(page);
		}
		if (size == null || "".equals(size)) {
			iSize = 10;
		} else {
			iSize = Integer.parseInt(size);
		}
		long total = roleService.count();
		mav.addObject("page", iPage);
		mav.addObject("pages", total % iSize == 0 ? total / iSize : total / iSize + 1);
		mav.addObject("size", iSize);
		mav.addObject("total", total);
		List<Role> list = roleService.findPage(iPage, iSize);
		mav.addObject("list", list);
		return mav;
	}

}
